package com.bear.cmcc.NetTestHTTP.ui.view;

import java.util.List;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.MenuBean;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.ui.activity.AboutActivity;
import com.bear.cmcc.NetTestHTTP.ui.activity.SettingActivity;
import com.bear.cmcc.NetTestHTTP.ui.adapter.MenuAdapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
/**
 * 系统菜单自定义
 * @author meizhen.xu
 *
 */
public class MenuLayout extends LinearLayout {
	
	private Context context;
	private GridView gridview;
	private MenuAdapter adapter;
	private OnItemMenuClickListener onItemMenuClickListener;
	
	public MenuLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}
	
	public MenuLayout(Context context){
		super(context);
		this.context = context;
		init();
	}
	
	private void init(){
		View view = LayoutInflater.from(context).inflate(R.layout.menu_layout, null);
		gridview = (GridView)view.findViewById(R.id.menuLayout_gridview);
		adapter = new MenuAdapter(context);
		gridview.setAdapter(adapter);
		gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				MenuBean b = adapter.getItem(position);
				onItemMenuClickListener.onItemClick(b);
				hide();
			}
			
		});
		
		this.addView(view, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	}
	
	/**
	 * 刷新菜单
	 * @param list
	 */
	public void refresh(List<MenuBean> list){
		if(list!=null){
			if(adapter.getCount()>0){
				adapter.clear();
			}
			for(int i=0;i<list.size();i++){
				MenuBean b = list.get(i);
				adapter.addObject(b);
			}
			adapter.notifyDataSetChanged();
			list.clear();
			list = null;
		}
	}
	
	public boolean isOpen(){
		if(this.getVisibility() == View.VISIBLE){
			return true;
		}else{
			return false;
		}
	}
	
	public void show(){
		this.setVisibility(View.VISIBLE);
	}
	
	public void hide(){
		this.setVisibility(View.GONE);
		
	}
	
	
	
	public void setOnItemMenuClickListener(
			OnItemMenuClickListener onItemMenuClickListener) {
		this.onItemMenuClickListener = onItemMenuClickListener;
	}



	public interface OnItemMenuClickListener{
		
		public void onItemClick(MenuBean b);
	}

}
